package cn.ffcs.bpmpvc.web;


import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.echoice.paper.PageBean;
import org.echoice.paper.PageNavi;
import cn.ffcs.base.web.BaseAction;
import cn.ffcs.bpm.model.DateControl;
import cn.ffcs.bpm.model.ProcTranRecord;
import cn.ffcs.bpm.model.ProcessConfigDetail;
import cn.ffcs.bpm.model.TransactProcessStatus;
import cn.ffcs.bpmpvc.dao.FlowPvcDao;
import cn.ffcs.bpmpvc.services.AppBusinessReqService;
import cn.ffcs.bpmpvc.services.AppDataTranferService;
import cn.ffcs.bpmpvc.svc.DateManager;
import cn.ffcs.bpmpvc.svc.ProcessPvcService;
import cn.ffcs.bpmpvc.svc.TaskService;
import cn.ffcs.bpmpvc.web.forms.FlowReqReportForm;
import cn.ffcs.businessrequest.model.BusinessRequest;
import cn.ffcs.security.model.User;
import cn.ffcs.util.context.ApplicationContextUtil;


public class FlowDateManagerAction<D extends TransactProcessStatus> extends
		BaseAction<D> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ProcessPvcService processPvcService;
	private TaskService taskService;
	private FlowReqReportForm flowReqReportForm;
	private AppBusinessReqService appBusinessReqService;
	private AppDataTranferService appDataTranferService;
	
	/**
	 * 可配置时间管控的需求单列表
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String dateBsReqList(){
		HttpServletRequest request = ApplicationContextUtil.getHttpServletRequest();
		int pageSize = PageNavUtil.getPageSize(request);
		FlowPvcDao flowPvcDao = getProcessPvcService().getFlowPvcDao();
		PageBean pageBean = getAppBusinessReqService().getAppBusinessReqDao().queryPageNeedWeek(flowReqReportForm, null,null,PageNavUtil.getPageNo(request), pageSize);
		List<BusinessRequest> bsReqList = pageBean.getDataList();
		if (bsReqList != null && bsReqList.size() > 0) {
			List<ProcTranRecord> procRecordList = null;
			int i = 0;
			for (BusinessRequest businessRequest : bsReqList) {
				// 取得流程运行中的任务记录
				procRecordList = flowPvcDao.findRunTaskList(businessRequest
						.getProcinstId());
				if (procRecordList != null && procRecordList.size() > 0) {
					for (ProcTranRecord procTranRecord : procRecordList) {
						if ("active".equals(procTranRecord.getState())) {
							businessRequest.setCurrStatus(procTranRecord.getProcessStep().getName());
							businessRequest.setCurrUser(procTranRecord.getTransactor().getName());
						}
					}
				} else {
					businessRequest.setCurrUser("");
					businessRequest.setCurrStatus("结束");
				}
				i++;
			}
		}
		request.setAttribute("reportList", bsReqList);
		String pageNaviStr = PageNavi.getPageNaviText(request, pageBean);
		request.setAttribute("pageNaviStr", pageNaviStr);
		return "dateBsReqList";
	}
	/**
	 * 跳转配置窗口
	 * @return
	 */
	public String toDeploy(){
		HttpServletRequest request = ApplicationContextUtil.getHttpServletRequest();
		String reqId=request.getParameter("reqId");
		BusinessRequest businessRequest=getAppBusinessReqService().getAppBusinessReqDao().getBusinessRequestById(Integer.parseInt(reqId));
		String processId=getProcessPvcService().getFlowPvcDao().getFlowProcessId(businessRequest.getProcinstId().toString());
		List<ProcessConfigDetail> processDetailList=getProcessPvcService().getFlowPvcDao().findAllProcessDetail(Integer.parseInt(processId));
		List<DateControl> controlList=getAppBusinessReqService().getAppBusinessReqDao().findDateContolByReqId(reqId);
		request.setAttribute("processDetailList", processDetailList);
		request.setAttribute("businessRequest", businessRequest);
		request.setAttribute("controlList", controlList);
		return "dateDeploy";
	}
	/**
	 * 保存
	 * @return
	 */
	public String deploy(){
		HttpServletRequest request = ApplicationContextUtil.getHttpServletRequest();
		if(getAppDataTranferService().intoDateControl(request)==false){
			this.returnMsg="下一个流程的管控时间应该不小于当前流程的管控时间";
			return "msg";
		}
		return dateBsReqList();
	}
	/**
	 * 时间管控报表
	 * @return
	 */
	public String dateManagerList(){
		HttpServletRequest request = ApplicationContextUtil.getHttpServletRequest();
		int pageSize = PageNavUtil.getPageSize(request);
		return findBsReqReport(pageSize);
	}
	@SuppressWarnings("unchecked")
	public String findBsReqReport(int pageSize) {
		HttpServletRequest request = ApplicationContextUtil.getHttpServletRequest();	
//        User user =ApplicationContextUtil.getUserContextObject().getCurrUser();
		String reqName=request.getParameter("reqName");
		if(reqName!=null&&!"".equals(reqName)){
			request.setAttribute("reqName", reqName.trim());
		}
		PageBean pageBean = getAppBusinessReqService().getAppBusinessReqDao().queryPageBsReqReportList(null,reqName,PageNavUtil.getPageNo(request),pageSize);
		List<BusinessRequest> bsReqList = pageBean.getDataList();
		List<DateManager> dateList=getAppBusinessReqService().getAppBusinessReqDao().dateList();
		request.setAttribute("reportList", bsReqList);
		request.setAttribute("dateList", dateList);
		String pageNaviStr = PageNavi.getPageNaviText(request, pageBean);
		request.setAttribute("pageNaviStr", pageNaviStr);
		return SUCCESS;
	}
	
	public ProcessPvcService getProcessPvcService() {
		return processPvcService;
	}
	public void setProcessPvcService(ProcessPvcService processPvcService) {
		this.processPvcService = processPvcService;
	}
	public TaskService getTaskService() {
		return taskService;
	}
	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}
	public AppBusinessReqService getAppBusinessReqService() {
		return appBusinessReqService;
	}
	public void setAppBusinessReqService(AppBusinessReqService appBusinessReqService) {
		this.appBusinessReqService = appBusinessReqService;
	}
	public AppDataTranferService getAppDataTranferService() {
		return appDataTranferService;
	}
	public void setAppDataTranferService(AppDataTranferService appDataTranferService) {
		this.appDataTranferService = appDataTranferService;
	}

	public FlowReqReportForm getFlowReqReportForm() {
		return flowReqReportForm;
	}

	public void setFlowReqReportForm(FlowReqReportForm flowReqReportForm) {
		this.flowReqReportForm = flowReqReportForm;
	}
	
}
